package com.huobao.yunying.fragments;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.huobao.yunying.adapters.Adapter_DetailCommit;
import com.huobao.yunying.entity.CommitDetail;
import com.huobao.yunying.entity.Detail_images;
import com.huobao.yunying.https.Http;
import com.huobao.yunying.https.HttpListener;
import com.huobao.yunying.https.HttpUtil;
import com.huobao.yunying.views.NoScrollListView;
import com.huobao.zhangying.R;
import com.mcoy.snapscrollview.McoyScrollView;

import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.ScrollView;
import android.widget.TextView;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

public class Tuwen_pinglun_Fragment extends BaseFragment implements RadioGroup.OnCheckedChangeListener{

	private String goodsid;
	private Handler handler = new Handler();
	public McoyScrollView scrollView;
	private NoScrollListView listview;
	private RadioGroup rg;
	private Adapter_DetailCommit adapter;
	private TextView tv_empty;
	public static Tuwen_pinglun_Fragment getInstant(String id){
		Tuwen_pinglun_Fragment instance = new Tuwen_pinglun_Fragment();
		Bundle args = new Bundle();
		args.putString("goodsid",id);
		instance.setArguments(args);
		return instance;
	}
	@Override
	public View onCreateContentView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.layout_detail_pinglun, null);
		this.goodsid = getArguments().getString("goodsid");
		scrollView = (McoyScrollView)view.findViewById(R.id.scrollview);
		tv_empty = (TextView)view.findViewById(R.id.tv_empty);
		rg = (RadioGroup)view.findViewById(R.id.rg);
		rg.setOnCheckedChangeListener(this);
		listview = (NoScrollListView)view.findViewById(R.id.pl_lv);
		adapter = new Adapter_DetailCommit(getActivity());
		listview.setAdapter(adapter);
		((RadioButton)view.findViewById(R.id.rb1)).setChecked(true);
		handler.post(new Runnable() {
			
			@Override
			public void run() {
				getCommit("");
				showContentView();
			}
		});
		return view;
	}


	@Override
	public void onCheckedChanged(RadioGroup radioGroup, int i) {
		switch(i){
			case R.id.rb1://全部
				getCommit("");
				break;
			case R.id.rb2://好评
				getCommit("3");
				break;
			case R.id.rb3://中评
				getCommit("2");
				break;
			case R.id.rb4://差评:
				getCommit("1");
				break;

		}
	}

	private void getCommit(String dengji){
		Map<String,String> map = HttpUtil.getParams("app","goods","act","goodscomment","id",goodsid,"dengji",dengji);
		Http.getInstant().sendRequest(getActivity(), map, new HttpListener() {
			@Override
			public void onHttpSuccess(String result) {
				JSONObject jsonObject = JSON.parseObject(result);
				List<CommitDetail> list = JSON.parseArray(jsonObject.getString("retval"), CommitDetail.class);
				adapter.clear();
				adapter.addAll(list);
				if(adapter.isEmpty()){
					tv_empty.setVisibility(View.VISIBLE);
				}else{
					tv_empty.setVisibility(View.GONE);
				}
			}

			@Override
			public void onHttpError(String error) {

			}
		});
	}
}
